package com.campsys.controller;

import com.campsys.core.Result;
import com.campsys.core.TableData;
import com.campsys.domain.TSysUser;
import com.campsys.domain.param.PageParam;
import com.campsys.domain.param.UserAddParam;
import com.campsys.domain.param.UserLoginParam;
import com.campsys.domain.param.UserUpdateParam;
import com.campsys.domain.vo.ActivityCategoryListVo;
import com.campsys.domain.vo.AdminPageVo;
import com.campsys.mapping.ActivityMapping;
import com.campsys.mapping.UserMapping;
import com.campsys.service.IUserService;
import com.campsys.util.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @ApiOperation("登录")
    @PostMapping("/login")
    public Result login(@RequestBody UserLoginParam userLoginParam){
        return Result.success(userService.createToken(userLoginParam.getLoginName(),userLoginParam.getPassword()));
    }

//    @PreAuthorize("@lcc.myAuthority('user:register')")
//    @ApiOperation("注册")
//    @PostMapping("/register")
//    public Result register(){
//        return Result.success();
//    }

    @ApiOperation("获得活动管理员分页列表")
    @PreAuthorize("@lcc.myAuthority('user:admin:page')")
    @GetMapping("/admin/page")
    public TableData<AdminPageVo> getAdminPage(PageParam pageParam){
        return TableData.success(userService.getAdminPage(pageParam),userService.getAdminPageTotal());
    }


    @ApiOperation("获得账户信息")
    @PreAuthorize("@lcc.myAuthority('user:info')")
    @GetMapping("/info")
    public Result info(HttpServletRequest request){
        return Result.success(userService.getUserInfoByUserId(JWTUtil.getUserId(request.getHeader("Authorization"))));
    }

    @ApiOperation("增加活动管理员")
    @PreAuthorize("@lcc.myAuthority('user:add:admin')")
    @PostMapping("/add/admin")
    public Result addAdmin(@RequestBody UserAddParam userAddParam){
        boolean rs = userService.addAdmin(userAddParam);

        return rs ? Result.success() : Result.error();
    }

    @ApiOperation("修改活动管理员")
    @PreAuthorize("@lcc.myAuthority('user:update:admin')")
    @PutMapping("/update/admin")
    public Result updateAdmin(@RequestBody UserUpdateParam userUpdateParam){
        boolean rs = userService.updateById(UserMapping.INSTANCE.to(userUpdateParam));

        return rs ? Result.success() : Result.error();
    }

    @ApiOperation("删除活动管理员")
    @PreAuthorize("@lcc.myAuthority('user:delete:admin')")
    @PutMapping("/delete/admin/{id}")
    public Result deleteAdmin(@PathVariable Long id){
        TSysUser user = new TSysUser();
        user.setId(id);
        user.setStatus("2");
        boolean rs = userService.updateById(user);

        return rs ? Result.success() : Result.error();
    }



}
